<?php


namespace App\Http\Repositories\Common\LaravelExcel;


use App\Exports\InvoicesExport;
use App\Http\Repositories\BaseRepository;
use App\Imports\ImportsExcel;
use App\Jobs\ExportsExcelJob;
use App\Models\Local\UserModel;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;

class ExcelRepository extends BaseRepository
{

    public function imports($data)
    {
//        return (new UserModel())->get();
        $file = $data['excel'];
        $extension = $file->getClientOriginalExtension();
//        dd($extension);
        $path = $file->getRealPath();
        if ($extension == 'xls' || $extension == 'xlsx' || $extension == 'txt') {
            $format = [
                'name' => '姓名',
                'sex' => '性别',
                'email' => '邮箱',
                'phone' => '号码',
                'age' => '年龄',
                'address' => '地址',
                'status' => '状态',
                'create_time' => '时间'
            ];
            Excel::import(new ImportsExcel($format, new UserModel()), $path);

            return [];

        } else {
            $this->throwError([0, '不是excel文件 .xls .xlsx .csv']);
        }
    }

    public function exports($data)
    {
        $data = DB::connection('local')
            ->table('user')
            ->limit(10000)
            ->get()
            ->toArray();
            $format = [
                    '序号',
                    '姓名',
                    '性别',
                    '邮箱',
                    '号码',
                    '年龄',
                    '地址',
                    '状态',
                    '时间'
            ];

        $export = new InvoicesExport($data, $format);
        return Excel::download($export, '111.xls');

        dispatch((new ExportsExcelJob($data, '2020', $format, 'A1', 'csv')))->onQueue('excel');
        return [];
    }


}
